package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.PrescriptionMouldAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.Prescription;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/5/24 12:06
 * 功能：
 */
public class PrescriptionMouldActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.add_button)
    Button addButton;

    private PrescriptionMouldAdapter adapter;
    private List<Prescription> lists;

    private Integer logo;
    private String ask_id,p_id,z_type,temp_id;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_prescription_mould;
    }

    @Override
    protected void initViews() {
        setToolbar(R.id.tool_bar);
        ButterKnife.bind(this);

        logo = Integer.parseInt(getIntent().getStringExtra("logo"));
        z_type = this.getIntent().getStringExtra("type");
        temp_id = this.getIntent().getStringExtra("temp_id");

        lists = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new PrescriptionMouldAdapter(mActivity,handler,logo);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

              Bundle bundle = new Bundle();
              bundle.putString("temp_id",lists.get(listPosition).getId()+"");
              bundle.putString("logo","1");
              startActivity(PrescriptionMouldDetailsActivity.class,bundle);

            }
        });

        if (logo == 1){

            ask_id = getIntent().getStringExtra("ask_id");
            p_id = getIntent().getStringExtra("p_id");
            addButton.setText("在线开方");

        }else{

            addButton.setText("添加模板");

        }


        getMakeTemp();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @OnClick(R.id.add_button)
    public void onViewClicked() {

        Intent intent = new Intent();
        intent.putExtra("logo",logo+"");
        intent.putExtra("ask_id", ask_id);
        intent.putExtra("p_id", p_id);
        intent.putExtra("z_type", z_type);
        intent.putExtra("temp_id", temp_id);
        intent.setClass(mActivity, SelectPrescriptionActivity.class);
        startActivity(intent);

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    Log.e("书记","书记"+msg.obj.toString());

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");
                        Gson gson = new Gson();
                        for (int i=0;i<array.length();i++){

                            Prescription p = gson.fromJson(array.getJSONObject(i).toString(),Prescription.class);
                            lists.add(p);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;

                case 2:

                    Intent intent = new Intent();
                    intent.putExtra("temp_id", msg.obj.toString());
                    mActivity.setResult(Activity.RESULT_OK,intent);
                    finish();

                    break;

            }

        }
    };

    public void getMakeTemp() {


        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(null)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getMakeTemp");
        params.put("data[]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.make/entry", params, handler, 1, 404);

    }

}
